# -*- coding: utf-8 -*-
# @Time : 2023/10/7 3:45 PM 
# @Author : Wayne
# @Email : 1
# @File : default_data.py
# @Project : python_demo
# @DESCRIPTION :

"""
字段不存在就给对应默认值
"""

def handle_null_value(value):
    if value is None:
        return get_default_value(type(value))
    elif isinstance(value, str) and value == '':
        return get_default_value(str)
    elif isinstance(value, int) and value == 0:
        return get_default_value(int)
    elif isinstance(value, float) and value == 0.0:
        return get_default_value(float)
    elif isinstance(value, list) and len(value) == 0:
        return get_default_value(list)
    elif isinstance(value, dict) and len(value) == 0:
        return get_default_value(dict)
    else:
        return value

def get_default_value(data_type):
    default_values = {
        int: 0,
        str: '',
        float: 0.0,
        list: [],
        dict: {}
    }
    return default_values.get(data_type)

# 示例使用
field1 = None  # int 类型
field2 = ''    # str 类型
field3 = 0.0   # float 类型
field4 = []    # list 类型
field5 = {}    # dict 类型

field1 = handle_null_value(field1)
field2 = handle_null_value(field2)
field3 = handle_null_value(field3)
field4 = handle_null_value(field4)
field5 = handle_null_value(field5)

print(field1)  # 输出: 0
print(field2)  # 输出: ''
print(field3)  # 输出: 0.0
print(field4)  # 输出: []
print(field5)  # 输出: {}
